
package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Query;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.dao.MallShopsGoodsDao;
import com.platform.modules.mall.entity.MallGoodsEntity;
import com.platform.modules.mall.entity.MallShopsEntity;
import com.platform.modules.mall.entity.MallShopsGoodsEntity;
import com.platform.modules.mall.service.MallActivityService;
import com.platform.modules.mall.service.MallShopsGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 店铺与商品关联关系Service实现类
 *
 * @author
 * @date 2019-07-04 09:46:59
 */
@Service("mallShopsGoodsService")
public class MallShopsGoodsServiceImpl extends ServiceImpl<MallShopsGoodsDao, MallShopsGoodsEntity> implements MallShopsGoodsService {

    @Resource
    private MallActivityService activityService;

    @Override
    public Page getShopCardList(Map<String, Object> params) {
        if(StringUtils.isBlank((String)params.get("shopId"))){
            throw new BusinessException("店铺ID不能为空！");
        }
        params.put("sidx", "B.SORT");
        params.put("asc", false);
        Page<Map<String,Object>> page = new Query<Map<String,Object>>(params).getPage();
        // 分页查询门店下的卡券
        page.setRecords(baseMapper.getShopCardList(page,params));
        // 获取卡券的活动信息
        page.getRecords().forEach(x->{
            String activityId =  (String) x.get("activityId");
            if(StringUtils.isNotBlank(activityId)){
                x.put("activity", activityService.getActivityDetails(activityId,x.get("id").toString()));
            }
        });
        return page;
    }

    @Override
    public List<MallShopsGoodsEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public List<MallShopsGoodsEntity> selectMallShopsGoodsCategory(Map<String, Object> params) {
        return baseMapper.selectMallShopsGoodsCategory(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.ID");
        params.put("asc", false);
        Page<MallShopsGoodsEntity> page = new Query<MallShopsGoodsEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallShopsGoodsPage(page, params));
    }

    @Override
    public boolean add(MallShopsGoodsEntity mallShopsGoods) {
        return this.save(mallShopsGoods);
    }

    @Override
    public boolean add(MallGoodsEntity mallGoodsEntity) {
        MallShopsGoodsEntity mallShopsGoods = new MallShopsGoodsEntity();
        mallShopsGoods.setGoodsId(mallGoodsEntity.getId());
        mallShopsGoods.setShopsId(mallGoodsEntity.getShopsId());
        mallShopsGoods.setRetailPrice(mallGoodsEntity.getRetailPrice());
        mallShopsGoods.setSort(mallGoodsEntity.getSort());
        return this.save(mallShopsGoods);
    }

    @Override
    public boolean update(MallShopsGoodsEntity mallShopsGoods) {
        return this.updateById(mallShopsGoods);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }


    @Override
    public List<MallShopsEntity> selectShopsByGoodsId(String goodsId) {
        return this.baseMapper.selectShopsByGoodsId(goodsId);
    }
}
